package top.meeting.room.common.kit;

import com.xiaoleilu.hutool.date.DateUtil;
import top.meeting.room.module.book.dto.Timer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Lyt on 2017/9/12.
 */
public class TimeKit {
    /**
     * 时间就近取整:<=30分向前取整,>30分向后取整
     * Author:lyt
     *
     * @param time inTime 07:56
     * @return outTime 08:00
     */
    private static String getInitialTime(String time) {
        String hour = "00";//小时
        String minutes = "00";//分钟
        String outTime = "00:00";
        StringTokenizer st = new StringTokenizer(time, ":");
        List<String> inTime = new ArrayList<String>();
        while (st.hasMoreElements()) {
            inTime.add(st.nextToken());
        }
        hour = inTime.get(0).toString();
        minutes = inTime.get(1).toString();
        if (Integer.parseInt(minutes) > 30) {
            hour = (Integer.parseInt(hour) + 1) + "";
        }
        outTime = hour + ":00";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            outTime = sdf.format(sdf.parse(outTime));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return outTime;
    }

    public static String[] getTimeLine() {
        String[] timeLine = {"09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30",
                "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00"};
        return timeLine;
    }

    /**
     * 计算两个时间之间相差多少个0.5小时
     *
     * @param startTimeStr
     * @param endTimeStr
     * @return
     */
    public static long getTimeDiff(String startTimeStr, String endTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = sdf.parse(startTimeStr);
            endDate = sdf.parse(endTimeStr);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        long nh = (1000 * 60 * 60) / 2;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - startDate.getTime();
        // 计算差多少小时
        long hour = diff / nh - 1;
        return hour;
    }

    public static String getDayOfWeek(int day) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        int dayOfWeek = day - 1;
        return weekDays[dayOfWeek];
    }

    public static List<Timer> getTimer() {
        List<Timer> timerList = new ArrayList<Timer>();
        String[] timeLine = getTimeLine();
        for (int i = 0; i < timeLine.length - 1; i++) {
            timerList.add(Timer.builder().doing("暂无").timeLine(timeLine[i] + "-" + timeLine[i + 1]).openId("").status("").build());
        }
        return timerList;
    }

    public static Timer[] getNullTimer() {
        Timer[] timers = new Timer[18];
        for (int i = 0; i < timers.length; i++) {
            timers[i] = new Timer(getTimeLine()[i] + "-" + getTimeLine()[i + 1], "暂无");
        }
        return timers;
    }

    public static void main(String[] args) throws java.text.ParseException {
        int dayOfWeek = DateUtil.dayOfWeek(new Date());
        System.out.println(dayOfWeek);
    }
}
